﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SksApp
{
    public partial class frmListe : Form
    {
        public enum ListeTipleri
        {
            StokListesi = 0,
            CariListesi = 1,
            BekleyenEvraklar = 2,
            FisListesi = 3,
            SaticiListesi = 4
        }

        public ListeTipleri ListeTipi { get; set; }

        public string ResultValue { get; set; }

        public frmListe()
        {
            InitializeComponent();
        }
        private void KeyClick(object sender, EventArgs e)
        {
            Button btn = sender as Button;
            txtFilter.Text += btn.Text;
            txtFilter.Select(txtFilter.TextLength, 0);
        }

        private void SpaceClick(object sender, EventArgs e)
        {
            txtFilter.Text += " ";
            txtFilter.Select(txtFilter.TextLength, 0);
        }

        private void btnSil_Click(object sender, EventArgs e)
        {
            if (txtFilter.TextLength > 0)
                txtFilter.Text = txtFilter.Text.Remove(txtFilter.Text.Length - 1, 1);
            txtFilter.Select(txtFilter.TextLength, 0);
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Abort;
        }

        private void btnSec_Click(object sender, EventArgs e)
        {
            Secim();
        }

        private void Secim()
        {
            if (dataGridView1.RowCount > 0 && dataGridView1.CurrentRow.Index > -1)
            {
                this.ResultValue = dataGridView1[0, dataGridView1.CurrentRow.Index].Value.ToString();
                DialogResult = System.Windows.Forms.DialogResult.OK;
            }
            else
                DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }

        private void frmListe_Load(object sender, EventArgs e)
        {
            if (ListeTipi == ListeTipleri.StokListesi)
            {
                cmbFiltre.Items.Add("Stok Kodu");
                cmbFiltre.Items.Add("Stok Adı");
            }
            else if (ListeTipi == ListeTipleri.CariListesi)
            {
                cmbFiltre.Items.Add("Müşteri Kodu");
                cmbFiltre.Items.Add("Müşteri Adı");
            }
            cmbFiltre.SelectedIndex = 1;
        }

        private void Listele()
        {
            using (SksEntity ent = new SksEntity())
            {
                if (ListeTipi == ListeTipleri.StokListesi)
                {
                    if (cmbFiltre.SelectedIndex == 0)
                    {
                        var liste = ent.Database.SqlQuery<ListStok>(string.Format("SELECT Barkod.Barkodu, Barkod.BirimAdi, Stok.Adi, Stok.Fiyat1 FROM Barkod INNER JOIN Stok ON Barkod.StokID = Stok.ID WHERE (Stok.Kodu like '{0}%')", txtFilter.Text.Replace('*', '%')));
                        dataGridView1.DataSource = liste.Select(x => new { x.Barkodu, x.BirimAdi, UrunAdi = x.Adi, Fiyati = x.Fiyat1 }).ToList();
                    }
                    else
                    {
                        var liste = ent.Database.SqlQuery<ListStok>(string.Format("SELECT Barkod.Barkodu, Barkod.BirimAdi, Stok.Adi, Stok.Fiyat1 FROM Barkod INNER JOIN Stok ON Barkod.StokID = Stok.ID WHERE (Stok.Adi like '{0}%')", txtFilter.Text.Replace('*', '%')));
                        dataGridView1.DataSource = liste.Select(x => new { x.Barkodu, x.BirimAdi, UrunAdi = x.Adi, Fiyati = x.Fiyat1 }).ToList();
                    }
                }
                else if (ListeTipi == ListeTipleri.CariListesi)
                {
                    if (cmbFiltre.SelectedIndex == 0)
                    {
                        var liste = ent.Musteriler.SqlQuery(string.Format("SELECT * FROM Musteri WHERE (Kodu like '{0}%')", txtFilter.Text.Replace('*', '%')));
                        dataGridView1.DataSource = liste.Select(x => new { x.ID, MusteriKodu = x.Kodu, MusteriAdi = x.Adi, VergiDaire = x.VergiDairesi, VergiNo = x.VergiNo }).ToList();
                    }
                    else
                    {
                        var liste = ent.Musteriler.SqlQuery(string.Format("SELECT * FROM Musteri WHERE (Adi like '{0}%')", txtFilter.Text.Replace('*', '%')));
                        dataGridView1.DataSource = liste.Select(x => new { x.ID, MusteriKodu = x.Kodu, MusteriAdi = x.Adi, VergiDaire = x.VergiDairesi, VergiNo = x.VergiNo }).ToList();
                    }

                    dataGridView1.Columns[0].Visible = false;
                }
            }
            dataGridView1.AutoResizeColumns();
        }

        private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                Secim();
            }
        }

        private void dataGridView1_DoubleClick(object sender, EventArgs e)
        {
            Secim();
        }

        private void btnAra_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtFilter.Text))
            {
                Listele();
            }
        }

        private void txtFilter_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (!string.IsNullOrEmpty(txtFilter.Text))
                {
                    Listele();
                }
            }
            else if (e.KeyCode == Keys.Down)
            {
                dataGridView1.Focus();
            }
        }
    }
}
